<?php

/**
 * 公共方法及配置类
 */
class Controller extends CController
{

	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
    public $navbarName='';
    public $navbarSubName='';
    public $retData=array('code'=>200,'msg'=>'','obj'=>array());
    // public $layout='//layouts/main';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

    public $layout = '/layouts/main';//设置默认调用layouts
    public $SEO = array('keywords' => '' ,'description' => ''); //seo配置
    public $left_list_on="1"; //后台左侧默认高亮
    public $nav_list_on="1"; //前台导航默认高亮
    public $page_num=25;//分页条数

    public $public_title=array('cn'=>'-盛睿德','en'=>'-srd');
    /**
     * 跳转提示窗口
     *
     * @param string $link 地址
     * @param string $info 说明文字
     * @param int    $time 跳转等待秒数
     * @param string $moreInfo
     */
    public function redirectStay($link ,$info ,$time = 3 ,$moreInfo = '')
    {
        $this->widget('application.components.InfoHref' ,array('info' => $info ,'link' => $link ,'time' => $time ,'moreInfo' => $moreInfo));
        exit;
    }

    /**
     *上传图片
     * @param        $imgpath  文件$_file
     * @param        $fileurl  保存文件目录
     * @param string $username
     *
     * @return bool|string
     */
    public function upload_img($imgpath,$fileurl,$username='')
    {
        if( !$this->makeDir($fileurl)){
            return false;
        }
        $name = $imgpath["name"];
        $tmp_name = $imgpath["tmp_name"];
        $type = $imgpath["type"];
        $size = $imgpath["size"];
        $img_name = date('Ymd').'_'.uuid();
        $uploadfile = $fileurl . $img_name . '.' . $this->extend($imgpath["name"]);
        //$uploadfile = time()."_".$imgpath['name'];

        $maxsize = 1600 * 1200;                                           //最大允许上许文件大小
        if($name == "")                                             //文件名为空
        {
            echo "<script>alert('请先选择要上传的图片文件!');
			 window.history.back();</script>";
        }
        if($type != "image/pjpeg" && $type != "image/jpeg" && $type != "image/png")//文件类型不在指定范围
        {
            echo "<script>alert('上传文件只可以是JPEG或PNG类型的!');
			window.history.back();</script>";
            // exit;
        }
        if($size > $maxsize)                                       //超过规定大小
        {
            echo "<script>alert('上传文件大小不能超过2M! ');window.history.back();</script>";
            // exit;
        }
        if(move_uploaded_file($tmp_name ,$uploadfile)){
            //mkdir($uploadfile ,0644);
            return $uploadfile;
        }else if(copy($tmp_name ,$uploadfile)){
            //mkdir($uploadfile ,0644);
            return $uploadfile;
        }else{
            return false;
        }
    }
    /**
     * 取文件后缀名
     * @param $file_name 文件路径
     *
     * @return string
     */
    public function extend($file_name){
        $extend =explode("." , $file_name);
        $va=count($extend)-1;
        return $extend[$va];
    }
    /**
     * 创建多级目录
     * @param     $dir
     * @return boolcreate_folders
     */
    function makeDir($dir){
        return is_dir($dir) or ((dirname($dir)) and mkdir($dir, 0777));
    }
    /**
     * 生成数组文件内容
     * @param     $array
     * @param int $level
     *
     * @return string
     */
    function arrayeval($array, $level = 0) {
        $space = '';
        for($i = 0; $i <= $level; $i++) {
            $space .= "\t";
        }
        $evaluate = "Array ( \n";
        //$comma = $space;
        $comma = '';
        foreach($array as $key => $val) {
            $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
            $val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '\''.addcslashes($val, '\'\\').'\'' : $val;
            if(is_array($val)) {
                $evaluate .=  "\t\n"."$comma$key => ".arrayeval($val, $level + 1);
            } else {
                $evaluate .= "$comma$key => $val";
            }
            $comma = ",\n";
        }
        $evaluate .= "\n)\n";
        return $evaluate;
    }
    /**
     * 写入文件
     * @param        $filename  文件路径及文件名
     * @param        $writetext 文件内容
     * @param string $openmod   权限
     *
     * @return bool
     */
    function swritefile($filename, $writetext, $openmod='w') {
        if($fp = fopen($filename, $openmod)) {
            flock($fp, 2);
            fwrite($fp, $writetext);
            fclose($fp);
            chmod($filename, 0755); //权限修改
            return true;
        } else {
            runlog('error', "File: $filename write error.");
            return false;
        }
    }

    function getLang(){
        if($_GET['lang']=='cn'){
            $lang='cn';
        }elseif($_GET['lang']=='en'){
            $lang='en';
        }else{
            $lang=Yii::app()->request->cookies['lang'];
        }
        if(empty($lang)){ $lang='cn'; }
        return $lang;
    }

    /**
     * 初始化类配置
     */
    public function  __construct(){
        $this->SEO['keywords']='';
//        $sql='select * from `test`';
//        $array  = Yii::app()->db->createCommand($sql)->queryAll();
//        print_r($array);
    }




    /**
     * 创建url链接
     *
     * @param string $url  路径
     * @param array  $params 参数
     */
    public function CreateHref($url,$params){
        $hosts= 'http://'.$_SERVER['HTTP_HOST']; //https nginx当前没有方式来判断是否是https协议
        $params_url='';
        if(is_array($params)){
            foreach($params as $k=>$v){
                if(empty($k)){ continue;}
                $params_url.='/'.$k.'/'.$v;
            }
        }
        //通过url来判断中的.html来判断是否链接后加.html
        if(strstr($url,'.html')){
            return $hosts.str_replace('.html','',$url).$params_url.'.html';
        }else{
            return $hosts.$url.$params_url;
        }
    }


}